﻿/******************************************************************************
 * Copyright (C) 671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef GAME_SERVER_WORLDSESSION_H_
#define GAME_SERVER_WORLDSESSION_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <ctime>
#include <NodeCpp/Macros.h>
#include <NodeCpp/AutoPtr.h>
#include <NodeCpp/LockedQueue.h>
#include "Shared/Typedef.h"
#include "Shared/Common.h"
#include "Shared/WorldPacket.h"
#include "Server/PacketFilter.h"
#include "Server/OpcodeHandler.h"
using NodeCpp::LockedQueue;
using NodeCpp::AutoPtr;

class WorldSocketDelegate;
class WorldSession;
class Player;

class WorldSession
{
public:
    WorldSession(std::uint32_t _ID, WorldSocketDelegate* _Conn,
        AccountTypes _Sec, time_t _MuteTime, LocaleConstant _Locale);
    ~WorldSession(void);

    void QueuePacket(WorldPacket* _Pkt) { Packets_.Add(_Pkt); }
    bool Update(PacketFilter& _Filter);

    std::uint32_t GetAccountId(void) const { return AccountId_; }
    void SetLatency(std::uint32_t _Latency) { Latency_ = _Latency; }
    void SetClientDelay(std::uint32_t _Delay) { ClientDelay_ = _Delay; }
    AccountTypes GetSecurity(void) const { return Security_; }
    Player* GetPlayer(void) const { return Player_; }
    bool IsPlayerLoading(void) const { return PlayerLoading_; }

    void SendPacket(WorldPacket& _Packet);
    void SendAuthOk(void);
    void SendAuthWaitQue(std::uint32_t _Position);

    /********************* 数据包处理程序 *********************/
    void HandleServerSide(WorldPacket& _Packet);
    void HandleCharEnumOpcode(WorldPacket& _Packet);
    void HandleCharEnum(QueryResult* _Rs);
    void HandleCharCreateOpcode(WorldPacket& _Packet);
    void HandlePlayerLoginOpcode(WorldPacket& _Packet);
    void HandlePlayerLogin(SqlQueryHolder* _Holder);

private:
    void _ExecuteOpcode(WorldPacket *_Packet, const OpcodeHandler &_Ophandler);
    void _Uninitialize(void);
    typedef LockedQueue<WorldPacket*> PacketQueue;

    std::string PeerHost_;

    std::uint32_t AccountId_;
    WorldSocketDelegate* Sock_;
    AccountTypes Security_;
    time_t MuteTime_;
    LocaleConstant Locale_;

    PacketQueue Packets_;
    bool Running_;

    std::uint32_t Latency_;
    std::uint32_t ClientDelay_;

    // 游戏世界 /////////////////////////////////////////////////////////////////
    std::uint32_t BillingTimeRemaining_;
    std::uint8_t BillingPlanFlags_;
    std::uint32_t BillingTimeRested_;

    // 正在加载角色
    Player* Player_;
    bool PlayerLoading_;
    DISALLOW_COPY_AND_ASSIGN(WorldSession);
};

#endif // GAME_SERVER_WORLDSESSION_H_
